<?php

$path = dirname(__FILE__);
$timestamp = $path . '/wvt.timestamp.cron';
if (file_get_contents($timestamp) + 3600 < time()) {
	require($path . '/config.wvt.php');
	require($path . '/libs/locale/' . $wvt_options['lang'] . '.php');
	$dir = opendir($wvt_options['videodir']);
	while ($file = readdir($dir)) {
		if (strpos($file, '.state')) {
			$state_file = $wvt_options['videodir'] . $file;
			$base_file = preg_replace('!\.state$!', '', $file);
			$state = @file_get_contents($state_file);
			$desc = @file_get_contents(str_replace($base_file . '.desc'));
			$desc = $desc ? $desc : $base_file;
			if ($state == 4) {
				$mp4 = $wvt_options['videodir'] . 'mp4/' . $base_file . '.mp4';
				$ogv = $wvt_options['videodir'] . 'theora/' . $base_file . '.ogv';
				if ((@is_file($mp4) && !@filesize($mp4)) || (@is_file($ogv) && !@filesize($ogv))) {
					@file_put_contents($state_file, '0');
					$state = 0;
				} else {
					@file_put_contents($state_file, '5');
				}
			}
			$subject = $body = '';
			switch ($state) {
				case 0:
					@file_put_contents($state_file, '2');
				case 4:
					$subject = str_replace('###DESC###', $desc, constant('_WVT_NOTIFY_SUBJECT' . $state));
					$body = str_replace('###DESC###', $desc, constant('_WVT_NOTIFY_BODY' . $state));
					break;
			}
			if ($subject) {
				$headers = 'From: ' . $wvt_options['email'] . "\r\n" .
					'Content-Type: text/plain; charset=utf-8' . "\r\n";
				@mail($wvt_options['email'], "=?utf-8?B?" . base64_encode($subject) . "?=", $body, $headers);
			}
		}
	}
	@file_put_contents($timestamp, time());
}

?>